﻿using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.Modularity;
using Microsoft.Practices.Unity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SurveillancePro.Base.Events;
using SurveillancePro.Base.Common;
using System.Diagnostics;
using System.Windows;
using ToolkitMgtModule.Views;
using ToolkitMgtModule.ViewModels;
using InteractiveWhiteboardManagement.Base.Common;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.ServiceLocation;
using EnvControlLib;
using SurveillanceLib;
using CommonBase;


namespace ToolkitMgtModule
{
    /// <summary>
    /// 工具箱模块
    /// </summary>
    public class ToolkitMgtModule : IModule
    {
        #region 常量

        private readonly IEventAggregator _eventAggregator;
        private readonly IUnityContainer _container;
        private readonly IRegionManager _regionManager;
        private readonly IServiceLocator _serviceLocator;

        #endregion

        #region 构造函数

        public ToolkitMgtModule(IEventAggregator eventAggregator, IUnityContainer container,
            IRegionManager regionManager, IServiceLocator serviceLocator)
        {
            this._eventAggregator = eventAggregator;
            this._container = container;
            this._regionManager = regionManager;
            this._serviceLocator = serviceLocator;
        }

        #endregion

        #region 公开的方法

        /// <summary>
        /// 注册模块内的事件
        /// </summary>
        public void Subcribe()
        {
            // 接收主模块的消息(ViewModel_Shell)
            OpenModelEvent openEvent = _eventAggregator.GetEvent<OpenModelEvent>();
            openEvent.Subscribe(Show, ThreadOption.UIThread, true,
                x => x.ModelName == this.GetType().Name);
        }

        /// <summary>
        /// 事件回调函数
        /// </summary>
        /// <param name="arg"></param>
        public void Show(OpenModelEventArg arg)
        {
            if (string.IsNullOrWhiteSpace(arg.FunctionName))
            {
                // 注册视图类型
                this._container.RegisterType<object, Views.View_Toolkit>(ViewNames.ToolkitMgtModule_Toolkit);
                this._container.RegisterType<object, Views.View_PlayBack>(ViewNames.ToolkitMgtModule_PlayBack);
                this._container.RegisterType<object, Views.View_UserConfig>(ViewNames.ToolkitMgtModule_UserConfig);

                return;
            }

            try
            {
                switch (arg.FunctionName)
                {
                    case "PlayBack": // 远程录像回放
                        if (arg.Arg != null && arg.Arg is EnvVideoPanel)
                        {
                            _playBack(arg.Arg as EnvVideoPanel);
                        }
                        else
                        {
#if DEBUG
                            Debug.WriteLine("远程录像回放：传入的视频窗口信息有问题！");
#endif
                            MessageBox.Show("远程录像回放：视频窗口信息异常！", ConstantString.MSG_COMPANY);
                        }
                        break;
                    

                    default:
                        break;
                }
            }
            catch (System.Exception ex)
            {
#if DEBUG
                Debug.WriteLine(ex.Message);
#endif
                MessageBox.Show(ex.Message, ConstantString.MSG_COMPANY);
            }
        }

        #endregion

        #region 私有方法

        /// <summary>
        /// 远程录像回放
        /// </summary>
        /// <param name="selectedPanel">当前所选视频窗口</param>
        private void _playBack(EnvVideoPanel selectedPanel)
        {
            View_PlayBack view = this._regionManager.Regions[RegionNames.ContentRegion].GetView(ViewNames.ToolkitMgtModule_PlayBack) as View_PlayBack; // 根据视图名获取对应视图
            if (view == null) // 若找不到对应视图则添加
            {
                View_PlayBack playBack = this._serviceLocator.GetInstance<Object>(ViewNames.ToolkitMgtModule_PlayBack) as View_PlayBack; // 解析远程录像回放界面
                this._regionManager.Regions[RegionNames.ContentRegion].Add(playBack, ViewNames.ToolkitMgtModule_PlayBack); // 添加新的视图到当前区域中 AddToRegion
                view = playBack;
            }

            if (view != null && view.DataContext != null &&
                view.DataContext is ViewModel_PlayBack && selectedPanel != null &&
                selectedPanel.Tag != null && selectedPanel.Tag is TagModel)
            {
                TagModel panelTag = selectedPanel.Tag as TagModel;
                ViewModel_PlayBack vm_PlayBack = view.DataContext as ViewModel_PlayBack;

                vm_PlayBack.SearchItemList(panelTag.Driver_IP, panelTag.Panel_SelectedChannel); // 查询文件列表
            }
            this._regionManager.Regions[RegionNames.ContentRegion].Activate(view); // 激活视图显示
        }

        #endregion

        #region IModule 成员

        public void Initialize()
        {
            Subcribe(); // 注册模块事件
        }

        #endregion
    }
}
